package com.threeox.ormlibrary.db;

import android.database.Cursor;

import com.alibaba.fastjson.JSONObject;
import com.threeox.ormlibrary.entity.TableColumnMsg;
import com.threeox.ormlibrary.entity.TableMsg;
import com.threeox.ormlibrary.impl.SqlExecutor;

import java.util.List;

/** 
 *
 * @ClassName: SuperSQLDataBaseDao
 *
 * @Description: (数据库操作Dao)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/16 15:52 
 * 
 * @version 1.0
 */
public class OrmSQLDataBaseDao {

	/**
	 * 查询说有的表格信息
	 * @return
     */
	public static JSONObject queryAllTableMsg() {
        SqlExecutor executor = SqlExecutor.newInstance();
        JSONObject data = new JSONObject();
		String sql = "select * from sqlite_master where type = 'table';";
        List<TableMsg> tableMsgs = executor.queryList(sql, TableMsg.class);
		for (TableMsg tableMsg : tableMsgs) {
            Cursor column = executor.getReadableDatabase().query(tableMsg.getTbl_name(), null, null, null, null, null, "1");
            if (column != null) {
                tableMsg.setColumnNames(column.getColumnNames());
                column.close();
            }
			data.put(tableMsg.getTbl_name(), tableMsg);
		}
		return data;
	}

	/**
	 * 根据表名的所有的列信息
	 *
	 * @param executor
	 * @param tableName
	 * @return
	 */
	public static List<TableColumnMsg> queryColumnsByTable(SqlExecutor executor, String tableName) {
        return executor.queryList("pragma table_info(" + tableName + ")", TableColumnMsg.class);
	}

}
